<!DOCTYPE html>
<html  lang="en">
<head>
    <meta charset="utf-8">
<title>rust基础2 - noback</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />



    <meta name="description" content="rust基础变量常量rust采用多种变量的定义形式，同时在定义的同时区分了变量与常量的区别  定义变量使用let xx&#x3D;xx  # 默认immutable 定义可变变量 let mut xx&#x3D;xx  # mut 可变 定义常量 const xx: u32 &#x3D; 10000 # 常量定义 名为xx  类型为int32 值为10000 其中常量默认不变 fn main() {     let x &#x3D; 5">
<meta property="og:type" content="article">
<meta property="og:title" content="rust基础2">
<meta property="og:url" content="http://alpaca-h.gitee.io/2019/12/26/blog_back_new/Rust/rust%E5%9F%BA%E7%A1%80/60-rust%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="noback">
<meta property="og:description" content="rust基础变量常量rust采用多种变量的定义形式，同时在定义的同时区分了变量与常量的区别  定义变量使用let xx&#x3D;xx  # 默认immutable 定义可变变量 let mut xx&#x3D;xx  # mut 可变 定义常量 const xx: u32 &#x3D; 10000 # 常量定义 名为xx  类型为int32 值为10000 其中常量默认不变 fn main() {     let x &#x3D; 5">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://alpaca-h.gitee.io/images/og_image.png">
<meta property="article:published_time" content="2019-12-26T04:27:09.000Z">
<meta property="article:modified_time" content="2019-12-26T04:58:48.003Z">
<meta property="article:author" content="Alpaca">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://alpaca-h.gitee.io/images/og_image.png">







<link rel="icon" href="/images/favicon.svg">


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.7.2/css/bulma.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:400,600|Source+Code+Pro">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@9.12.0/styles/androidstudio.css">


    
    
    
    <style>body>.footer,body>.navbar,body>.section{opacity:0}</style>
    

    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/css/lightgallery.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/css/justifiedGallery.min.css">
    

    
    

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.css">


    
    
    
    

<link rel="stylesheet" href="/css/back-to-top.css">


    
    

    
    
    
    

    
    
<link rel="stylesheet" href="/css/progressbar.css">
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

    
    
    

    
    
    
        <script async="" src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    

    


<link rel="stylesheet" href="/css/style.css">
<meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="noback" type="application/atom+xml">
</head>
<body class="is-3-column">
    <nav class="navbar navbar-main">
    <div class="container">
        <div class="navbar-brand is-flex-center">
            <a class="navbar-item navbar-logo" href="/">
            
                <img src="/images/logo.svg" alt="rust基础2" height="28">
            
            </a>
        </div>
        <div class="navbar-menu">
            
            <div class="navbar-start">
                
                <a class="navbar-item"
                href="/">Home</a>
                
                <a class="navbar-item"
                href="/archives/">Archives</a>
                
                <a class="navbar-item"
                href="/categories/">Categories</a>
                
                <a class="navbar-item"
                href="/tags/">Tags</a>
                
                <a class="navbar-item"
                href="/about/">About</a>
                
            </div>
            
            <div class="navbar-end">
                
                    
                    
                    <a class="navbar-item" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>
                        
                    </a>
                    
                
                
                <a class="navbar-item is-hidden-tablet catalogue" title="Catalogue" href="javascript:;">
                    <i class="fas fa-list-ul"></i>
                </a>
                
                
                <a class="navbar-item search" title="Search" href="javascript:;">
                    <i class="fas fa-search"></i>
                </a>
                
            </div>
        </div>
    </div>
</nav>
    
    <section class="section">
        <div class="container">
            <div class="columns">
                <div class="column is-8-tablet is-8-desktop is-9-widescreen has-order-2 column-main"><div class="card">
    
    <div class="card-content article ">
        <h1 class="title is-size-3 is-size-4-mobile has-text-weight-normal">
            
                <i class="fas fa-angle-double-right"></i>rust基础2
            
        </h1>
        
        <div class="level article-meta is-size-7 is-uppercase is-mobile is-overflow-x-auto">
            <div class="level-left">
                <time class="level-item has-text-grey" datetime="2019-12-26T04:27:09.000Z"><i class="far fa-calendar-alt">&nbsp;</i>2019-12-26</time>
                
                <time class="level-item has-text-grey is-hidden-mobile" datetime="2019-12-26T04:58:48.003Z"><i class="far fa-calendar-check">&nbsp;</i>2019-12-26</time>
                
                
                <div class="level-item">
                <i class="far fa-folder-open has-text-grey"></i>&nbsp;
                <a class="has-link-grey -link" href="/categories/blog-back-new/">blog_back_new</a>&nbsp;/&nbsp;<a class="has-link-grey -link" href="/categories/blog-back-new/Rust/">Rust</a>
                </div>
                
                
                <span class="level-item has-text-grey">
                    <i class="far fa-clock"></i>&nbsp;
                    
                    
                    6 minutes read (About 856 words)
                </span>
                
                
                <span class="level-item has-text-grey" id="busuanzi_container_page_pv">
                    <i class="far fa-eye"></i>
                    <span id="busuanzi_value_page_pv">0</span> visits
                </span>
                
            </div>
        </div>
        
        <div class="content">
            <h1 id="rust基础"><a href="#rust基础" class="headerlink" title="rust基础"></a>rust基础</h1><h2 id="变量常量"><a href="#变量常量" class="headerlink" title="变量常量"></a>变量常量</h2><p>rust采用多种变量的定义形式，同时在定义的同时区分了变量与常量的区别 </p>
<pre><code class="bash">定义变量使用let xx=xx  # 默认immutable
定义可变变量 let mut xx=xx  # mut 可变
定义常量 const xx: u32 = 10000 # 常量定义 名为xx  类型为int32 值为10000</code></pre>
<p>其中常量默认不变</p>
<pre><code class="go">fn main() {
    let x = 5;
    println!(&quot;The value of x is:{}&quot;,x);
    let mut y = 6;
    println!(&quot;The mut value of y is:{}&quot;,y);

    //修改不可变量x  报错
    //x = 7
    //println!(&quot;i upadte the x value 5 to {}&quot;,x)

    y = 10;
    println!(&quot;i update the y value 6 to {}&quot;,y);

    //定义常量,他的名字是MAX_POINTS
    const MAX_POINTS: u32 = 100_000;
    println!(&quot;is constants value  MAX_POINTS: {}&quot;,MAX_POINTS);

    //变量迭代 隐藏
    //对变量的隐藏，也就是不断的对x进行隐藏
    //虽然变量x是不可变的immutable 但是重复使用let可以多次进行隐藏，生成之后的仍就是immutable
    let q = 5;
    let q = q + 5;
    let q = q + 10;
    println!(&quot;q:{}&quot;,q)

    //
    let spaces = &quot;    &quot;;
    let spaces_line = spaces.len();
    println!(&quot;spance_lines_number:{}&quot;,spaces_line)

    //
    let mut numbers = &quot;   &quot;;
    numbers = numbers.len();
    println!(&quot;{}&quot;,numbers)
}</code></pre>
<p><font color='red'>可变量mut 和隐藏shadowing有什么区别?</font><br>mut 与隐藏的另一个区别是，当再次使用 let 时，实际上创建了一个新变量，我们可以改变值的类型，并且复用这个名字。例如，假设程序请求用户输入空格字符来说明希望在文本之间显示多少个空格，然而我们真正需要的是将输入存储成数字（多少个空格）</p>
<h2 id="datatype数据类型"><a href="#datatype数据类型" class="headerlink" title="datatype数据类型"></a>datatype数据类型</h2><p>rust是静态类型语言(statically typed)<br>也就是说在编译时就必须知道所有变量的类型。</p>
<h3 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h3><pre><code class="bash"></code></pre>
<pre><code class="bash">度    有符号    无符号
8-bit    i8    u8
16-bit    i16    u16
32-bit    i32    u32
64-bit    i64    u64
128-bit    i128    u128
arch    isize    usize</code></pre>
<p>有符号和无符号代表数字能否为负值，有符号数以补码形式（two’s complement representation）[<a href="https://kaisery.github.io/trpl-zh-cn/ch03-02-data-types.html]" target="_blank" rel="noopener">https://kaisery.github.io/trpl-zh-cn/ch03-02-data-types.html]</a> 存储。</p>
<h3 id="浮点型"><a href="#浮点型" class="headerlink" title="浮点型"></a>浮点型</h3><p>它们是带小数点的数字。Rust 的浮点数类型是 f32 和 f64，分别占 32 位和 64 位。默认类型是 f64，因为在现代 CPU 中，它与 f32 速度几乎一样，不过精度更高。</p>
<pre><code class="rust">fn main(){
    let x = 2.0; //f64
    let y: f32 = 3.0; //f32
} </code></pre>
<h3 id="数值运算"><a href="#数值运算" class="headerlink" title="数值运算"></a>数值运算</h3><pre><code class="rust">fn main(){
    let sum = 5 + 32;
    let difference = 95.5 - 4.3;
    let product = 4 * 30;
    let quotient = 56.7 / 32.2;
    let remainder = 43 % 5;
}</code></pre>
<h3 id="布尔型"><a href="#布尔型" class="headerlink" title="布尔型"></a>布尔型</h3><pre><code class="rust">fn main(){
    let t = true ;
    let f: boole = false; // 显式指定类型注解
} </code></pre>
<h3 id="字符型"><a href="#字符型" class="headerlink" title="字符型"></a>字符型</h3><p>char使用单引号 string使用双引号</p>
<pre><code class="bash">fn main() {
    let c = &#39;z&#39;;
    let z = &#39;ℤ&#39;;
    let heart_eyed_cat = &#39;😻&#39;;
} </code></pre>
<h3 id="元组类型"><a href="#元组类型" class="headerlink" title="元组类型"></a>元组类型</h3><p>元组是一个将多个其他类型的值组合进一个复合类型的主要方式。元组长度固定：一旦声明，其长度不会增大或缩小。</p>
<pre><code class="rust">fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
} </code></pre>
<pre><code class="rust">fn main() {
    let tup = (500, 6.4, 1);

    let (x, y, z) = tup;

    println!(&quot;The value of y is: {}&quot;, y);
} </code></pre>
<p>使用解构拆分元祖tup 对应元祖值</p>
<pre><code class="rust">fn main() {
    let x: (i32, f64, u8) = (500, 6.4, 1);

    let five_hundred = x.0;

    let six_point_four = x.1;

    let one = x.2;
} </code></pre>
<h3 id="数组类型"><a href="#数组类型" class="headerlink" title="数组类型"></a>数组类型</h3><pre><code class="bash">fn main() {
    # 定义方法1
    let a = [1, 2, 3, 4, 5];
    # 定义方法2
    let a: [i32; 5] = [1, 2, 3, 4, 5];
    # 定义方法3
    let a = [3;5] # =&gt; [3,3,3,3,3]
} </code></pre>
<pre><code class="bash">fn main() {
    let a = [1, 2, 3, 4, 5];

    let first = a[0];
    let second = a[1];
}
</code></pre>
<h2 id="函数方法"><a href="#函数方法" class="headerlink" title="函数方法"></a>函数方法</h2><pre><code class="bash">fn main() {
    println!(&quot;Hello, world!&quot;);

    another_function();
}

fn another_function() {
    println!(&quot;Another function.&quot;);
} 

# 加参数
fn main() {
    another_function(5);
}

fn another_function(x: i32) {
    println!(&quot;The value of x is: {}&quot;, x);
}

# 多参数
fn main() {
    another_function(5, 6);
}

fn another_function(x: i32, y: i32) {
    println!(&quot;The value of x is: {}&quot;, x);
    println!(&quot;The value of y is: {}&quot;, y);
}</code></pre>

        </div>
        
            <ul class="post-copyright">
            <li><strong>本文标题：</strong><a href="http://alpaca-h.gitee.io/2019/12/26/blog_back_new/Rust/rust%E5%9F%BA%E7%A1%80/60-rust%E5%9F%BA%E7%A1%80/">rust基础2</a></li>
            <li><strong>本文作者：</strong><a href="http://alpaca-h.gitee.io">Alpaca</a></li>
            <li><strong>本文链接：</strong><a href="http://alpaca-h.gitee.io/2019/12/26/blog_back_new/Rust/rust%E5%9F%BA%E7%A1%80/60-rust%E5%9F%BA%E7%A1%80/">http://alpaca-h.gitee.io/2019/12/26/blog_back_new/Rust/rust%E5%9F%BA%E7%A1%80/60-rust%E5%9F%BA%E7%A1%80/</a></li>
            <li><strong>发布时间：</strong>2019-12-26</li>
            <li><strong>版权声明：</strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！
            </li>
            </ul>
        
        
        
        
    </div>
</div>





<div class="card card-transparent">
    <div class="level post-navigation is-flex-wrap is-mobile">
        
        <div class="level-start">
            <a class="level level-item has-link-grey  article-nav-prev" href="/2019/12/26/xx/">
                <i class="level-item fas fa-chevron-left"></i>
                <span class="level-item">xx</span>
            </a>
        </div>
        
        
        <div class="level-end">
            <a class="level level-item has-link-grey  article-nav-next" href="/2019/12/26/blog_back_new/Rust/rust%E5%9F%BA%E7%A1%80/1-start/">
                <span class="level-item">Rust基础</span>
                <i class="level-item fas fa-chevron-right"></i>
            </a>
        </div>
        
    </div>
</div>



</div>
                




<div class="column is-4-tablet is-4-desktop is-3-widescreen  has-order-1 column-left ">
    
        
<div class="card widget">
    <div class="card-content">
        <nav class="level" style="margin-bottom:1rem">
            <div class="level-item has-text-centered">
                <div>
                    
                        <img class="image is-96x96 has-mb-6" src="https://www.gravatar.com/avatar/e0f4032c0f2d1068ffffbaf93c0bef52?s=96" alt="Xinyu Liu">
                    
                    
                    <p class="is-size-4 is-block">
                        Xinyu Liu
                    </p>
                    
                    
                    <p class="is-size-6 is-block">
                        Alpha Lxy
                    </p>
                    
                    
                    <p class="is-size-6 is-flex is-flex-center has-text-grey">
                        <i class="fas fa-map-marker-alt has-mr-7"></i>
                        <span>Beijing, China</span>
                    </p>
                    
                </div>
            </div>
        </nav>
        <nav class="level menu-list is-mobile" style="margin-bottom:1rem">
            <div class="level-item has-text-centered is-marginless">
                <a href="/archives/">
                    <p class="heading">
                        Posts
                    </p>
                    <p class="title has-text-weight-normal">
                        40
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/categories/">
                    <p class="heading">
                        Categories
                    </p>
                    <p class="title has-text-weight-normal">
                        13
                    </p>
                </a>
            </div>
            <div class="level-item has-text-centered is-marginless">
                <a href="/tags/">
                    <p class="heading">
                        Tags
                    </p>
                    <p class="title has-text-weight-normal">
                        0
                    </p>
                </a>
            </div>
        </nav>
        <div class="level">
            <a class="level-item button is-link is-rounded" href="https://www.github.com/AlphaLxy" target="_blank">
                <i class="fab fa-github"></i>&nbsp;&nbsp;Follow</a>
        </div>
        
        
    </div>
</div>

    
        
<div class="card widget column-left is-sticky" id="toc">
    <div class="card-content">
        <div class="menu">
            <h3 class="menu-label">
                Catalogue
            </h3>
            <ul class="menu-list"><li>
        <a class="is-flex" href="#rust基础">
        <span class="has-mr-6">1</span>
        <span>rust基础</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#变量常量">
        <span class="has-mr-6">1.1</span>
        <span>变量常量</span>
        </a></li><li>
        <a class="is-flex" href="#datatype数据类型">
        <span class="has-mr-6">1.2</span>
        <span>datatype数据类型</span>
        </a><ul class="menu-list"><li>
        <a class="is-flex" href="#整型">
        <span class="has-mr-6">1.2.1</span>
        <span>整型</span>
        </a></li><li>
        <a class="is-flex" href="#浮点型">
        <span class="has-mr-6">1.2.2</span>
        <span>浮点型</span>
        </a></li><li>
        <a class="is-flex" href="#数值运算">
        <span class="has-mr-6">1.2.3</span>
        <span>数值运算</span>
        </a></li><li>
        <a class="is-flex" href="#布尔型">
        <span class="has-mr-6">1.2.4</span>
        <span>布尔型</span>
        </a></li><li>
        <a class="is-flex" href="#字符型">
        <span class="has-mr-6">1.2.5</span>
        <span>字符型</span>
        </a></li><li>
        <a class="is-flex" href="#元组类型">
        <span class="has-mr-6">1.2.6</span>
        <span>元组类型</span>
        </a></li><li>
        <a class="is-flex" href="#数组类型">
        <span class="has-mr-6">1.2.7</span>
        <span>数组类型</span>
        </a></li></ul></li><li>
        <a class="is-flex" href="#函数方法">
        <span class="has-mr-6">1.3</span>
        <span>函数方法</span>
        </a></li></ul></li></ul>
        </div>
    </div>
</div>


    
    
        <div class="column-right-shadow is-hidden-widescreen ">
        
        </div>
    
</div>

                
            </div>
        </div>
    </section>
    <footer class="footer">
    <div class="container">
        <div class="level">
            <div class="level-start has-text-centered-mobile">
                <a class="footer-logo is-block has-mb-6" href="/">
                
                    <img src="/images/logo.svg" alt="rust基础2" height="28">
                
                </a>
                <p class="is-size-7">
                &copy; 2020 Alpaca&nbsp;
                Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> & <a
                        href="http://github.com/ppoffice/hexo-theme-icarus" target="_blank">Icarus</a>
                
                <br>
                <span id="busuanzi_container_site_uv">
                Visited by <span id="busuanzi_value_site_uv">0</span> users
                </span>
                
                </p>
            </div>
            <div class="level-end">
            
                <div class="field has-addons is-flex-center-mobile has-mt-5-mobile is-flex-wrap is-flex-middle">
                
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
                        
                        <i class="fab fa-creative-commons"></i>&nbsp;<i class="fab fa-creative-commons-by"></i>&nbsp;<i class="fab fa-creative-commons-nc"></i>&nbsp;<i class="fab fa-creative-commons-sa"></i>&nbsp;
                        
                    </a>
                </p>
                
                <p class="control">
                    <a class="button is-white is-large" target="_blank" title="AlphaLxy GitHub" href="https://www.github.com/AlphaLxy">
                        
                        <i class="fab fa-github"></i>&nbsp;
                        
                    </a>
                </p>
                
                </div>
            
            </div>
        </div>
    </div>
</footer>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@2.22.2/min/moment-with-locales.min.js"></script>
<script>moment.locale("en");</script>


    
    
    
    <script src="/js/animation.js"></script>
    

    
    
    
    <script src="https://cdn.jsdelivr.net/npm/lightgallery@1.6.8/dist/js/lightgallery.min.js" defer></script>
    <script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js" defer></script>
    <script src="/js/gallery.js" defer></script>
    

    
    

<div id="outdated">
    <h6>Your browser is out-of-date!</h6>
    <p>Update your browser to view this website correctly. <a id="btnUpdateBrowser" href="http://outdatedbrowser.com/" target="_blank" rel="noopener">Update
            my browser now </a></p>
    <p class="last"><a href="#" id="btnCloseUpdateBrowser" title="Close">&times;</a></p>
</div>
<script src="https://cdn.jsdelivr.net/npm/outdatedbrowser@1.1.5/outdatedbrowser/outdatedbrowser.min.js" defer></script>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        outdatedBrowser({
            bgColor: '#f25648',
            color: '#ffffff',
            lowerThan: 'flex'
        });
    });
</script>


    
    
<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.5/unpacked/MathJax.js?config=TeX-MML-AM_CHTML" defer></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
    MathJax.Hub.Config({
        'HTML-CSS': {
            matchFontHeight: false
        },
        SVG: {
            matchFontHeight: false
        },
        CommonHTML: {
            matchFontHeight: false
        },
        tex2jax: {
            inlineMath: [
                ['$','$'],
                ['\\(','\\)']
            ]
        }
    });
});
</script>

    
    

<a id="back-to-top" title="Back to Top" href="javascript:;">
    <i class="fas fa-chevron-up"></i>
</a>
<script src="/js/back-to-top.js" defer></script>


    
    

    
    
    
    

    
    
    
    
    
    <script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js" defer></script>
    <script src="/js/clipboard.js" defer></script>
    

    
    
    

    


<script src="/js/main.js" defer></script>

    
    <div class="searchbox ins-search">
    <div class="searchbox-container ins-search-container">
        <div class="searchbox-input-wrapper">
            <input type="text" class="searchbox-input ins-search-input" placeholder="Type something..." />
            <span class="searchbox-close ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="searchbox-result-wrapper ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            CONTENT_URL: '/content.json',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script src="/js/insight.js" defer></script>
<link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/insight.css">
    
</body>
</html>